package org.catchmenow.play;

import java.util.*;

public class GameStatus 
{
	private int gridSize;
	private Vector<Pair<Integer, Integer> > positionOfPlayers;
	private int[][] positionOfTreasures;
	private Vector<Integer> treasuresOfEachPlayer;
	
	
	
	public GameStatus(int gridSize, int noOftresures)
	{
		this.gridSize = gridSize;
		initializeTreasures(noOftresures);
	}
	
	public void initializeTreasures(int noOfTreasures)
	{
		positionOfTreasures = new int[gridSize][gridSize];
		for (int i=0;i<gridSize;i++)
			for (int j=0;j<gridSize;j++)
				positionOfTreasures[i][j] = 0;
		
		Random randomGenerator = new Random();
		for (int k=0;k<noOfTreasures;k++)
		{
			int i = randomGenerator.nextInt(gridSize);
			int j = randomGenerator.nextInt(gridSize);
			positionOfTreasures[i][j]++;
		}
	}
	
	public int getNumberOftreasuresAtPosition(int i, int j)
	{
		return positionOfTreasures[i][j];
	}
	
	public int getGridSize() {
		return gridSize;
	}

	public void setGridSize(int gridSize) {
		this.gridSize = gridSize;
	}

	public Vector<Pair<Integer, Integer>> getPositionOfPlayers() {
		return positionOfPlayers;
	}

	public void setPositionOfPlayers(
			Vector<Pair<Integer, Integer>> positionOfPlayers) {
		this.positionOfPlayers = positionOfPlayers;
	}

	public Vector<Integer> getTreasuresOfEachPlayer() {
		return treasuresOfEachPlayer;
	}

	synchronized public int addPlayer()
	{
		int id = positionOfPlayers.size();
		if (id == (gridSize*gridSize))
		{
			System.out.println("Too many players");
			return -1;
		}
		Random rg = new Random();
		while(true)
		{
			int i = rg.nextInt(gridSize);
			int j = rg.nextInt(gridSize);
			if (!ispositionOccupied(i, j))
			{
				addPlayerToPosition(i,j);
				break;
			}
			
		}
		return 0;
	}
	
	public boolean ispositionOccupied(int x, int y)
	{
		for (int i=0;i<positionOfPlayers.size();i++)
		{
			if ((positionOfPlayers.get(i).getFirst() == x) && (positionOfPlayers.get(i).getSecond() == y))
				return true;
		}
		return false;
	}
	
	public void addPlayerToPosition(int x, int y)
	{
		positionOfPlayers.add(new Pair<Integer, Integer>(x,y));
	}
	
}
